import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


# 加入椒盐噪声
plt.subplot(221)
plt.title('orginal')
img = cv2.imread('road.png')
noise = np.random.randint(-1, 2, size=img.shape)*30
img_noise = img + noise
plt.imshow(img_noise)

# 均值滤波
plt.subplot(222)
plt.title('均值滤波')

img_mean = cv2.blur(img_noise, (9, 9))
plt.imshow(img_mean)

# 高斯滤波
plt.subplot(223)
plt.title('高斯滤波')

img_gaussian = cv2.GaussianBlur(img_noise.astype(np.uint8), (9, 9), 0)
plt.imshow(img_gaussian)

plt.subplot(224)
plt.title('中值滤波')
img_median = cv2.medianBlur(img_noise.astype(np.uint8), 9)
plt.imshow(img_noise)

plt.show()